<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Course;
use App\Models\Information;
use App\Models\Node;
use App\Models\Order;
use App\Models\Product;
use App\Models\VipUser;
use Illuminate\Support\Facades\Log;

class OrderService
{
    public function get($title)
    {
        $sql = Order::with('course')->when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('title','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function isPay($user_id,$id,$type){
        $order = Order::where('user_id',$user_id)->where('product_id',$id)->where('type',$type)->where('pay_status','success')->first();
        if($order){
            return true;
        }
        $date = Date("Y-m-d",time());
        $vipuser = VipUser::where('user_id',$user_id)->whereDate('end_date', '>' ,$date)->first();
        if($vipuser){
            return true;
        }
        return false;
    }

    public function payId($user_id,$type){
        return Order::where('user_id',$user_id)->where('type',$type)->where('pay_status','success')->pluck('product_id');
    }

    public function payCourse($user_id,$type){
        $order = Order::where('user_id',$user_id)->where('type',$type)->with('course')->where('pay_status','success')->get();
        return $order;
    }

    public function payCourseCount($user_id,$type){
        $order = Order::where('user_id',$user_id)->where('type',$type)->where('pay_status','success')->count();
        return $order;
    }

    public function insert($input){
        //判断金额information product
        if($input['type'] == 'course'){
            $goods = Course::select('price')->where('id',$input['product_id'])->first();
        }
        if($input['type'] == 'information'){
            $goods = Information::select('price')->where('id',$input['product_id'])->first();
        }
        if($input['type'] == 'product'){
            $goods = Product::select('price')->where('id',$input['product_id'])->first();
        }
        if($input['type'] == 'node'){
            $goods = Node::select('price')->where('id',$input['product_id'])->first();
        }
        if($goods){
            $input['amount'] = $goods->price;
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = Order::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = Order::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

}